import Cookies from 'js-cookie'
import Layout from '@/layout'
import JSEncrypt from 'jsencrypt'
import store from '@/store'
import { isObjectArray } from '.'

const TokenKey = 'TOMS_TOKEN'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  if (process.env.NODE_ENV === 'development') {
    return Cookies.set(TokenKey, token)
  }
  return Cookies.set(TokenKey, token, { domain: process.env.VUE_APP_DOMAIN })
}

export function removeToken() {
  if (process.env.NODE_ENV === 'development') {
    return Cookies.remove(TokenKey)
  } else {
    document.cookie = `${TokenKey}=;domain=${process.env.VUE_APP_DOMAIN};path=/;expires=Thu,01-Jan-1970 00:00:01 GMT`
  }
}

const res = []
export function fnAddDynamicMenuRoutes(menuList = [], routes = []) {
  var temp = []
  for (var i = 0; i < menuList.length; i++) {
    if (menuList[i].list && menuList[i].list.length >= 1) {
      temp = temp.concat(menuList[i].list)
    } else if (menuList[i].href && /\S/.test(menuList[i].href)) {
      menuList[i].href = menuList[i].href.replace(/^\//, '')
      var route = {
        path: menuList[i].href.replace('/', '-'),
        component: Layout,
        name: menuList[i].href.replace('/', '-'),
        meta: {
          menuId: menuList[i].menuId,
          title: menuList[i].menuTitle
        },
        children: [
          {
            path: 'dashboard',
            name: 'Dashboard',
            component: () => import('@/views/dashboard/index'),
            meta: { title: 'Dashboard', icon: 'dashboard', menuId: 2 }
          }
        ]
      }
      res.push(route)
    }
  }
  if (temp.length >= 1) {
    fnAddDynamicMenuRoutes(temp, routes)
  }
  return res
}

export function encryptPassword(json) {
  let random = ''
  for (var i = 0; i < 6; i++) {
    random += Math.floor(Math.random() * 10)
  }
  var encryptor = new JSEncrypt()
  var publicKey =
    'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkkApKkKERcT8C9pkcH7F2YxOyK8veLXqLm06iY+XlTuvSFCxC3Fe7AudJ01nj8BAXiI947/OTEHsbX8hQUIitJl1A/AFYiiqzd+ZzsQNPDrtXUMIGMFDa2KpYzSOCwLimZ4NHShXpynEjyer5jz55iL8LjwqBiPcHeg7IfA1Itfm4moONjaCBzMCtNw+5En6i4cS0f2Tilxh+8LLnryqThJiom64Yvu9NtLJLAYr5eGxQ6ng679AaD0nckXGOiy4HqgNA3LTGfq45L4wrCbLR2UcqPba9HJ90zamXz7elrgwFy1ShLCyLQ9+SjxGKKQpdxeqLvpttX+UY/+O4S8j2wIDAQAB'
  // var publicKey =
  //   'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArdkqUOdToCx+EaRa2H/gesPwgnIsYDSE5OwChH3h0hcQDX2XRj7jzac+s68q4eY6zocXUhJLYgu9Zx6H5Lp+OUCEPfX+QoXmR9AZM1zov+awCP1Cluh/GRoaXfxDYnN/LwRagQr5l05L7Z410sD0v6+6/U0YTyJlt3kq6suUzHpE/74AVe0lofTxgogn5dlxmdzqmCbnkW72NjvKU0EAERfvcR4Jj8tFovYM8Pjw1k6inRGT0Wec9JlzRB5C68+cDDqTlE69AFxT/C23UT7kDqvvzWAfpX6CYufjlgl+JkizeIwKfnSTNCeFzJCPtYRyY4dO0mV2MvZtahvyS7L4JwIDAQAB'
  encryptor.setPublicKey(publicKey)
  var rsaPassWord = encryptor.encrypt(JSON.stringify({ random, ...json }))
  return rsaPassWord
}

export function hasPermission(id) {
  const permissions = store.getters.btnPermissions
  if (isObjectArray(id)) {
    return id.some((_) => permissions.includes(_))
  } else {
    return permissions.includes(id)
  }
}
